package algorithms;

import java.util.ArrayList;
import java.util.List;

import util.BarCode;
import util.SensorColour;
import bluetooth.RemoteControl;
import lejos.robotics.navigation.DifferentialPilot;

public class BarCodeScanner {
	
	
	public final static int browncount = 8;
	private RemoteControl rc;

	
	
	public BarCodeScanner(RemoteControl rc){
		this.rc=rc;
	}
	public int scan() {
		DifferentialPilot pilot = rc.getPilot();
		double oldTravel = pilot.getTravelSpeed();
		
		pilot.setTravelSpeed(5);
		pilot.setAcceleration((int) (pilot.getMaxTravelSpeed()*4));
		orientateStartBarcode();
		ArrayList<SensorColour> points = scanPoints(pilot);
		goBackToMiddle(pilot);
		pilot.setTravelSpeed(oldTravel);
		pilot.setAcceleration(13);
		return createBarcode2(points);
		
		//performAction(barCode,pilot);
		
	}
	
	private void orientateStartBarcode()
	{
		rc.getPilot().backward();
		int brownCounter = 0;
		while(true)
		{
			if (rc.getLightColour() == SensorColour.BROWN)
				brownCounter++;
			else
				brownCounter = 0;
			
			if (brownCounter == browncount) {
				rc.immediateStop();
				break;
			}
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
//	public BarCode createBarcode(ArrayList<SensorColour> points){
//		int result = 0;
//		for(int i = 0; i < points.size(); i++)
//			if(points.get(i) == SensorColour.BROWN)
//				points.remove(i);
//		
//		double interval = points.size()/4.0;
//		int i = points.size()/8;
//		int modulo = 1;
//		while (i <= 7*points.size()/8){
//			double line = 0;
//			while (i <= interval) {
//			    if (points.get(i)==SensorColour.WHITE)
//			        line++;
//			    i++;
//			}
//			double colour = line/(points.size()/8.0);
//			if (Double.compare(colour, 0.5) > 0)
//				result = result+modulo;
//			interval = interval + points.size()/8.0;
//			modulo *= 2;
//		}
//
//		return new BarCode(result);
//	}
	
	public static int createBarcode2(ArrayList<SensorColour> points)
	{
		// remove brown from code
//		for(int i = 0; i < browncount; i++)
//				points.remove(points.size()-1);
		for(int i = 0; i<points.size();i++)
			if(points.get(i)==SensorColour.BROWN)
				points.remove(i);
		float subListSize = points.size() / 8.0F;
		int value = 0;
		String out = "";
		for (int i = 1; i < 7; i++)
		{
			int listStart = Math.round(i*subListSize);
			int listEnd = Math.round((i+1)*subListSize);
			
			int black = 0;
			int white = 0;
			for(int j = listStart; j < listEnd; j++)
			{
				if (points.get(j) == SensorColour.WHITE)
					white++;
				else if(points.get(j) == SensorColour.BLACK)
					black++;
			}
			
			
			if (white >= black) {
				value += Math.round(Math.pow(2, i-1));
				out += "1";
			}
			else
				out += "0";
		}
		
		System.out.println("Out is " + out);
		System.out.println("Value is " + value);
		
		return value;
	}

	private ArrayList<SensorColour> scanPoints(DifferentialPilot pilot){
		ArrayList<SensorColour> list = new ArrayList<SensorColour>();
		pilot.forward();
		boolean lus = true;
		boolean seenBlack = false;
		int brownCounter = 0;
		while(lus){
			list.add(rc.getLightColour());
			if(!seenBlack && rc.getLightColour()==SensorColour.BLACK)
				seenBlack = true;
			if(seenBlack)
			{
				if (rc.getLightColour() == SensorColour.BROWN)
					brownCounter++;
				else
					brownCounter = 0;
				
				if (brownCounter == browncount) {
					rc.immediateStop();
					lus = false;
				}
			}
				
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		return list;
		
	}
	
	private void goBackToMiddle(DifferentialPilot pilot)
	{
		pilot.forward();
		while(rc.getLightColour() != SensorColour.WHITE)
		{
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		pilot.travel(-20 + RemoteControl.SENSOR_TO_WHEEL_DISTANCE);
	}
	
	
}



